package com.atguigu.my.chapter03

/**
 *
 * @description: 构造器案例
 * @time: 2020-05-22 17:36
 * @author: baojinlong 
 **/
object ConDemo01 {
  def main(args: Array[String]): Unit = {
    val person = new Person("windy", 18)
    print(person)
    val person2 = new Person("windy", 32)
    print(person2)
  }
}

//主构造函数
class Person(inName: String, inAge: Int) {
  // 主构造器执行方法
  var name: String = inName
  var age: Int = inAge

  override def toString: String = {
    "信息如下姓名:" + this.name + " 年龄:" + this.age
  }

  def this(inName: String) {
    // 辅助构造器必须要显示调用主构造器,方法必须要和主构造器一样.根本原因是
    this(inName, 10)
    // 重新赋值
    this.name = inName
  }
}

class Person2 private() {
  var name: String = _
  var age: Int = _

  def this(name: String) {
    this
    this.name = name
  }
}
